﻿#ifndef USERMANAGER_H
#define USERMANAGER_H

#include <stdint.h>

#include <map>
#include "../nlohmann/json.hpp"
#include <mutex>

// def a user struct

typedef  nlohmann::json User;


class UserManager
{
public:
    // 删除拷贝构造函数和拷贝赋值操作符
    UserManager(UserManager const&) = delete;
    UserManager& operator=(UserManager const&) = delete;
    // 提供一个全局唯一的接口
    static UserManager& getInstance() {
        static UserManager instance;
        return instance;
    }
    bool isExist(uint64_t ID);
    bool isExist(const std::string& account);

    User& createNewUser(uint64_t ID, const std::string& account);

    // return certain user according to id number
    User& getTargetUser(uint64_t ID);
    // return certain user according to account
    User& getTargetUser(const std::string& account);

    // convert account to id
    uint64_t account2ID(const std::string & account);

    // delete a certain user according to id number
    void deleteTargetUser(uint64_t ID);

    // query if a user is online
    bool queryUserOnline(uint64_t ID);
    bool queryUserOnline(const std::string & account);


private:

    std::mutex lock;

    std::map<std::string, uint64_t> account_ID;
    std::map<uint64_t, User> userStore;


    UserManager();
};

#endif // USERMANAGER_H
